package com.company.project.robot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.robot.entity.IdiomEntity;
import com.company.project.robot.service.IdiomService;
import com.company.project.robot.vo.ReptilesVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.IOException;
import java.util.List;

import com.company.project.common.utils.DataResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 成语表
 *
 * @author zyf
 * @email 2102378781@qq.com
 * @date 2021-11-03 16:24:25
 */
@Controller
@RequestMapping("/")
@Api(tags = "成语表")
public class IdiomController {
    @Autowired
    private IdiomService idiomService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/idiom")
    public String idiomList() {
        return "idiom/list";
    }

    /**
     * 跳转到修改页面
     */
    @GetMapping("/index/idiom/info")
    public String idiomInfo(String id, Model model) {
        model.addAttribute("id", id);
        return "idiom/edit";
    }
    /**
     * 跳转至爬取页面
     */
    @GetMapping("/index/idiom/reptile")
    public String jokesReptile() {
        return "idiom/reptile";
    }


    @ApiOperation(value = "新增")
    @PostMapping("idiom/add")
    @ResponseBody
    public DataResult add(@RequestBody IdiomEntity idiom) {
        String wordS = String.valueOf(idiom.getWord().charAt(0));
        String wordE = String.valueOf(idiom.getWord().charAt(idiom.getWord().length()-1));
        idiom.setWordE(wordE);
        idiom.setWordS(wordS);
        idiom.setPinyinE(idiomService.getPinyin(wordE));
        idiom.setPinyinS(idiomService.getPinyin(wordS));
        idiomService.save(idiom);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("idiom/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        idiomService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("idiom/update")
    @ResponseBody
    public DataResult update(@RequestBody IdiomEntity idiom) {
        String wordS = String.valueOf(idiom.getWord().charAt(0));
        String wordE = String.valueOf(idiom.getWord().charAt(idiom.getWord().length()-1));
        idiom.setWordE(wordE);
        idiom.setWordS(wordS);
        idiom.setPinyinE(idiomService.getPinyin(wordE));
        idiom.setPinyinS(idiomService.getPinyin(wordS));
        idiomService.updateById(idiom);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("idiom/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody IdiomEntity idiom) {
        Page page = new Page(idiom.getPage(), idiom.getLimit());
        LambdaQueryWrapper<IdiomEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(IdiomEntity::getId, idiom.getId());
        IPage<IdiomEntity> iPage = idiomService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @GetMapping("/idiom/{id}")
    @ApiOperation(value = "查询详情接口")
    @ResponseBody
    public DataResult detailInfo(@PathVariable("id") String id) {
        return DataResult.success(idiomService.getById(id));
    }


    @ApiOperation(value = "导出模板")
    @GetMapping(value = "/idiom/export/exportExcel",  produces = { "application/json;charset=UTF-8" })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        idiomService.exportExcel(request,response);
    }

    @ApiOperation(value = "导入数据")
    @PostMapping(value = "/idiom/export/excelImport",  produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public DataResult excelImport(@RequestParam("file") MultipartFile file) throws Exception {

        return DataResult.success(idiomService.excelImport(file));
    }

    @ApiOperation(value = "爬虫爬取成语大全数据接口")
    @PostMapping(value = "/idiom/riddle",  produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public DataResult riddle(@RequestBody ReptilesVo reptilesVo) throws IOException {
        idiomService.listSave(reptilesVo);
        return DataResult.success();
    }
}
